﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Zsea.Browser;
namespace Helper
{
    public partial class Form1 : System.Windows.Forms. Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Start_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(urlBox.Text
                ))
            {
                MessageBox.Show("Url地址错误！", "地址错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            Start.Enabled = false;
            treeViewBox.Nodes.Clear();
            System.Threading.ThreadPool.QueueUserWorkItem((System.Threading.WaitCallback)delegate {
                Browser br = new Browser(true);
                var request=br.Request(string.Empty, urlBox.Text, Method.Get);
                var document = request.Document;
                //document.DocumentNode.
                showTree(treeViewBox.Nodes, document.DocumentNode.Children);

                Start.Invoke((MethodInvoker)delegate {
                    Start.Enabled = true;
                    
                });
                CookieBox.Invoke((MethodInvoker)delegate
                {
                    List<string> cookieString=new List<string>();
                    foreach(var cookie in request.CookieContainer.All())
                    {
                        cookieString.Add(string.Format("{0}={1}", cookie.Name, cookie.Value));
                    }
                    CookieBox.Text = string.Join(";", cookieString.ToArray());

                });
            });
        }

        private void showTree(TreeNodeCollection pNodes,Zsea.HTML.NodeCollection nc)
        {
            //List<TreeNode> nodes = null;
            //if (pNodes == null)
            //    nodes = new List<TreeNode>();
            for (int i = 0; i < nc.Count; i++)
            {
                TreeNode node = new TreeNode();
                node.Text = string.Format("<{0}>", nc[i].Name);
                node.Tag = nc[i];
                
                
                treeViewBox.Invoke((MethodInvoker)delegate
                {
                    //Start.Enabled = true;
                    pNodes.Add(node);
                    //pNodes.
                });
                showTree(node.Nodes, nc[i].Children);
                //if (pNodes != null)
                //{
                //    pNodes.Add(node);
                //}
                //else
                //{
                //    nodes.Add(node);
                //}
                
                
            }
            //return nodes;
        }

        private void treeViewBox_AfterSelect(object sender, TreeViewEventArgs e)
        {
            var node = e.Node.Tag as Zsea.HTML.Node;
            nodeContentBox.Text = node.OuterHTML;
            xPathBox.Text = node.XPath;
        }
    }
    
}
